//
//  HMCustomNavigationController.swift
//  WeiBo
//
//  Created by Feng on 2017/6/4.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

class HMCustomNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置屏幕边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    
    //重写pushViewController方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0
        {
            if viewControllers.count == 1
            {
                let title = viewControllers.first?.title
                
//                print(title!)
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
            else
            {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
            
            //title
            viewController.title = "当前显示的是第\(viewControllers.count + 1)级控制器"
            
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    //leftBarButtonItem点击事件
    func popAction()
    {
        self.popViewController(animated: true)
    }
    
    //边缘手势代理方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if self.viewControllers.count == 1
        {
            return false
        }
        
        return true
    }
}
